'use strict';

const Controller = require('egg').Controller;

class LeetCodeController extends Controller {
  async getLeetCodeList() {
    const { ctx } = this;
    const { list } = await ctx.service.leetcode.getLeetCodeList();
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async getLeetCodeInfo() {
    const { ctx } = this;
    const userId = ctx.params.id;
    const { list } = await ctx.service.leetcode.getLeetCodeInfo(userId);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async getLeetCodeUserInfo() {
    const { ctx } = this;
    const leetcodeId = ctx.params.id;
    const userId = ctx.state.user.id;
    const { data } = await ctx.service.leetcode.getLeetCodeUserInfo(
      leetcodeId,
      userId
    );
    return ctx.helper.success({
      ctx,
      data,
    });
  }

  async liekLeetCode() {
    const { ctx } = this;
    const leetcode_id = ctx.params.id;
    const user_id = ctx.state.user.id;
    if (leetcode_id && user_id) {
      const { insertSuccess } = await ctx.service.leetcode.liekLeetCode(
        Number(leetcode_id),
        Number(user_id)
      );
      return ctx.helper.success({
        ctx,
        msg: insertSuccess ? '点赞成功' : '点赞失败',
      });
    }
    return ctx.helper.paramErr({ ctx });
  }

  async disliekLeetCode() {
    const { ctx } = this;
    const leetcode_id = ctx.params.id;
    const user_id = ctx.state.user.id;
    if (leetcode_id && user_id) {
      const { updateSuccess } = await ctx.service.leetcode.disliekLeetCode(
        leetcode_id,
        user_id
      );
      return ctx.helper.success({
        ctx,
        msg: updateSuccess ? '取消点赞成功' : '取消点赞失败',
      });
    }
  }

  async collectLeetCode() {
    const { ctx } = this;
    const leetcode_id = ctx.params.id;
    const user_id = ctx.state.user.id;
    if (leetcode_id && user_id) {
      const { insertSuccess } = await ctx.service.leetcode.collectLeetCode(
        Number(leetcode_id),
        Number(user_id)
      );
      return ctx.helper.success({
        ctx,
        msg: insertSuccess ? '收藏成功' : '收藏失败',
      });
    }
    return ctx.helper.paramErr({ ctx });
  }

  async cancelCollectLeetCode() {
    const { ctx } = this;
    const leetcode_id = ctx.params.id;
    const user_id = ctx.state.user.id;
    if (leetcode_id && user_id) {
      const { updateSuccess } = await ctx.service.leetcode.cancelCollectLeetCode(
        leetcode_id,
        user_id
      );
      return ctx.helper.success({
        ctx,
        msg: updateSuccess ? '取消收藏成功' : '取消收藏失败',
      });
    }
  }

  async doneLeetCode() {
    const { ctx } = this;
    const leetcode_id = ctx.params.id;
    const user_id = ctx.state.user.id;
    if (leetcode_id && user_id) {
      const { insertSuccess } = await ctx.service.leetcode.doneLeetCode(
        Number(leetcode_id),
        Number(user_id)
      );
      return ctx.helper.success({
        ctx,
        msg: insertSuccess ? '成功' : '失败',
      });
    }
    return ctx.helper.paramErr({ ctx });
  }

  async cancelDoLeetCode() {
    const { ctx } = this;
    const leetcode_id = ctx.params.id;
    const user_id = ctx.state.user.id;
    if (leetcode_id && user_id) {
      const { updateSuccess } = await ctx.service.leetcode.cancelDoLeetCode(
        leetcode_id,
        user_id
      );
      return ctx.helper.success({
        ctx,
        msg: updateSuccess ? '成功' : '失败',
      });
    }
  }
}

module.exports = LeetCodeController;
